package net.wanglu.www.zzz.rule.frag;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.process.Morphology;
import edu.stanford.nlp.trees.CollinsHeadFinder;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TypedDependency;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
import edu.stanford.nlp.trees.tregex.TregexPattern;

public class ParticiplePhraseFragments extends _FragmentsRule {

	Pattern startWithParticiple = Pattern
			.compile("^(\\b\\w+/VBN\\b|\\b\\w+/VBG\\b)");
	Pattern startWithNNP = Pattern
	.compile("^(\\b\\w+/NNP\\b)");
	TregexPattern ROOT = TregexPattern.compile("/\\bROOT\\b/<:(/\\bNP\\b/</\\bVP\\b/)");
	TregexPattern VP = TregexPattern.compile("/\\bVP\\b/>(/\\bNP\\b/>/\\bROOT\\b/)</\\bVBG\\b|\\bVBN\\b/!</\\bVBP\\b/");
	TregexPattern ROOT0 = TregexPattern.compile("/\\bROOT\\b/<:(/\\bNP\\b/!</\\bVP\\b/)");

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb.append("可以通过在分词短语前面添加必要的主语和谓语, 来修改分词短语残缺句. 例如:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("误: Sunning themselves on the hot concrete until they heard human feet crashing down the sidewalk.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("正: The little lizards were sunning themselves on the hot concrete until they heard human feet crashing down the sidewalk.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("还可以把分词短语放在一个独立的主句后, 作为句子的一部分, 表达出完整的意思. 例如:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("误: All the while twirling her batons with the speed and ferocity of helicopter blades.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("正: The majorette marched at the front of the parade, all the while twirling her batons with the speed and ferocity of helicopter blades.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("也可以把分词短语放在一个句子之前, 作为句子的一部分, 表达出完整的意思. 例如:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("误: Sucked down the pipe with a hearty slurp.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("正: Sucked down the pipe with a hearty slurp, the dirty bath water drained from the tub.");
		sb.append("</p>");
		return sb.toString();
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		Tree root  = (Tree) preprocessData.get("root");
		ArrayList<?> twl = (ArrayList<?>) preprocessData.get("twl");
		Collection<?> tdl = (Collection<?>) preprocessData.get("tdl");
		String sentence = (String) preprocessData.get("sentence");
		
		sb.clear();
		for(Iterator<?> itr = twl.iterator(); itr.hasNext();) {
			TaggedWord tw = (TaggedWord) itr.next();
			sb.append(tw.toString()).append(" ");
		}
		
		if ( startWithParticiple.matcher(sb.toString().trim()).find()) {
			Tree participleNode = root.headPreTerminal(new CollinsHeadFinder());
			for( Iterator<?> itr = tdl.iterator(); itr.hasNext();) {
				TypedDependency TD = (TypedDependency) itr.next();
				String reln  = TD.reln().getShortName();
				if ( "nsubj".equals(reln)) {
					if ( TD.gov() == participleNode) {
						return null;
					}
				}
			}
			BadEgg egg = new BadEgg();
			egg.setViolatedRule(this);
			egg.whereIsWrong = sentence;
			return egg;
		} else if ( ROOT.matcher(root).find()) {
			TregexMatcher m = VP.matcher(root);
			if ( m.find()) {
				BadEgg egg = new BadEgg();
				egg.setViolatedRule(this);
				egg.whereIsWrong = sentence;
				return egg;
			} else {
				return null;
			}
		} else if ( startWithNNP.matcher(sb.toString().trim()).find()) {
			TaggedWord tw = (TaggedWord) twl.get(0);
			String word = tw.word();
			String wordVBN = Morphology.lemmaStatic(word, "VBN", false);
			String wordVBG = Morphology.lemmaStatic(word, "VBG", false);
			if ( !word.equals(wordVBN) || !word.equals(wordVBG)) {
				if ( ROOT0.matcher(root).find()) {
					BadEgg egg = new BadEgg();
					egg.setViolatedRule(this);
					egg.whereIsWrong = sentence;
					return egg;
				}
			}
			return null;
		}

		return null;
	}
	
	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb
				.append("分词短语残缺句是以动词的过去分词或现在分词开头. 虽然有很多单词与动词分词构成分词短语, 但分词短语仍然不是一个独立的句子, 无法表达一个完整的意思.");
		sb.append("</p>");
		return sb.toString();
	}
}
